<?php

/*CONFIGURATION START*/

define("SITETITLE","PHP FTP WEB CLIENT");//meta tage site title
define("SITEKEYWORD","Creotec document download helper");//meta tag keyword
define("SITEDESC","Creotec document download");//meta tag description

define("SHOWPATH","yes");//show full path in frontend (yes/no)
define("IGNOREFILES","Thumbs.db"); //files not to show - full fileman

/*********************************************************************************/
//Marian :  need to change these configuration parameters for correct depolyments
define("PATHSECUREFILE","/var/www/simpleftp/ftpwebclient");//path to the 2 secure files SESSIONSTORE and PASSWORD

define("STOREFILE","usersession.txt");//path to the usersession.txt file - if its missing create an empty file
define("PASSWORDFILE","userlogin.txt");//path to the userlogin.txt file - if its missing create a file

define("COOKIEKEY","uyiwer87marianky74838_weu_re");//change this to anything the longer the better use a-z,0-9 etc

define("CLIENT_IP", "localhost"); //backend client application ip address that the php will use to connect to
define("CLIENT_PORT", "50555"); //backend client application port number
define("PRIM_DNS_IP", "1234");  //ip address of primary dns server
define("PRIM_DNS_PORT", "1234"); //port number of backup dns server
define("BACK_DNS_IP", "1234"); //ip address of backup dns server
define("BACK_DNS_PORT", "1234"); //port number of backup dns server

/*********************************************************************************/

/*CONFIGURATION END*/


/****** DO NOT EDIT BELOW THIS LINE *******/


/*FUNCTIONS START*/

function emailchksend($to,$from,$subject,$message) {
	if(ereg( "^([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$",$to)) {
		$headers  = "MIME-Version: 1.0\r\n";
		$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
		$headers .= "From: $from\r\n";
		mail($to,$subject,$message,$headers);
	}
}

function const_array($constant) {
  $array = explode(",",$constant);
  return $array;
};

function force_download($filename) {
	$dir=base64_decode($_GET['x'])."/";
	if (isset($filename) AND $filename != STOREFILE AND $filename != PASSWORDFILE AND $filename != 'config.php') {
		$file=$dir.$filename;
		header("Content-type: application/force-download");
		header("Content-Transfer-Encoding: Binary");
		header("Content-length: ".filesize($file));
		header("Content-disposition: attachment; filename=\"".basename($file)."\"");
		readfile("$file");
	} else {
		echo "No file selected";
	}
}

function getDirectorySize($path) {
  $totalsize = 0;
  $totalcount = 0;
  $dircount = 0;
  if ($handle = opendir ($path))
  {
    while (false !== ($file = readdir($handle)))
    {
      $nextpath = $path . '/' . $file;
      if ($file != '.' && $file != '..' && !is_link ($nextpath))
      {
        if (is_dir ($nextpath))
        {
          $dircount++;
          $result = getDirectorySize($nextpath);
          $totalsize += $result['size'];
          $totalcount += $result['count'];
          $dircount += $result['dircount'];
        }
        elseif (is_file ($nextpath))
        {
          $totalsize += filesize ($nextpath);
          $totalcount++;
        }
      }
    }
  }
  closedir ($handle);
  $total['size'] = $totalsize;
  $total['count'] = $totalcount;
  $total['dircount'] = $dircount;
  return $total;
}

function sizeformat($size){
    if($size<1024)   {
        return $size." bytes";
    }  else if($size<(1024*1024))  {
        $size=round($size/1024,0);
        return $size." KB";
    }  else if($size<(1024*1024*1024)) {
        $size=round($size/(1024*1024),0);
        return $size." MB";
    }  else  {
        $size=round($size/(1024*1024*1024),0);
        return $size." GB";
    }
}

function getFiles($path) {
   //Function takes a path, and returns a numerically indexed array of associative arrays containing file information,
   //sorted by the file name (case insensitive).  If two files are identical when compared without case, they will sort
   //relative to each other in the order presented by readdir()
   $files = array();
   $fileNames = array();
   $i = 0;

   if (is_dir($path)) {
       if ($dh = opendir($path)) {
           while (($file = readdir($dh)) !== false) {
               if ($file == "." AND $file == "..") continue;
               $fullpath = $path . "/" . $file;
               $myilist = IGNOREFILES.",".STOREFILE.",".PASSWORDFILE.",config.php";
               if (filetype($fullpath) != "dir" AND !in_array($file,const_array($myilist))) {
				   $fkey = strtolower($file);
				   while (array_key_exists($fkey,$fileNames)) $fkey .= " ";
				   $a = stat($fullpath);
				   $files[$fkey]['size'] = $a['size'];
				   $files[$fkey]['name'] = $file;
				   $files[$fkey]['type'] = filetype($fullpath);
				   $files[$fkey]['date'] = date ("Y/m/d H:i", filemtime($fullpath));
				   $fileNames[$i++] = $fkey;
		   		}
           }
           closedir($dh);
       } else die ("Cannot open directory:  $path");
   } else die ("Path is not a directory:  $path");
   sort($fileNames,SORT_STRING);
   $sortedFiles = array();
   $i = 0;
   foreach($fileNames as $f) $sortedFiles[$i++] = $files[$f];

   return $sortedFiles;
}

function chopstring($text,$len,$breaker = " ") {
	$vertext = strrchr(substr("$text",0,$len),"$breaker");
	$mylen = strlen ($vertext);
	$mylen = $len-$mylen;
	$newtext = substr($text,0,$mylen);
	if (strlen ($text) > $len) {
		return $newtext." ... ";
	} else {
		return $newtext;
	}
}

function readstoredsession($euser) {
	$filename = PATHSECUREFILE."/".STOREFILE;
    $fp = fopen($filename,'r');
	if(!$fp) {
	    echo 'Error, the file could not be opened';
	    exit;
	}
    $file_contents = fread($fp, filesize($filename));
    fclose($fp);
    $lines = explode ("\n", $file_contents);
    foreach ($lines as $line) {
        list($emailaddress,$mylist) = explode('|',$line);
		if ($emailaddress == "$euser") {
			return $mylist;
			break;
		}
    }
}

function storesession($euser,$sess) {
	$filename = PATHSECUREFILE."/".STOREFILE;
	$lines = file($filename);
	foreach($lines as $line_num => $line) {
		list($emailaddress,$mylist) = explode('|',$line);
		if ($emailaddress == "$euser") {
			unset($lines[$line_num]);
		}
	}
	$fp = fopen($filename, 'w');
	if(!$fp) {
	    echo 'Error, the file could not be opened';
	    exit;
	}
	fwrite($fp, implode("\n", $lines).$euser."|"."$sess\n");
	fclose($fp);
}

function read_dir($dir,$parent=NULL) {
   $mypath = opendir($dir);
   while (false !== ($file = readdir($mypath))) {
       if($file!="." && $file!="..") {
           if(!is_file($dir."/".$file))
               $dirs[]= $file;
       }
   }
   if($dirs) {
       natcasesort($dirs);
       foreach($dirs as $dir) {
           if ($parent) {
           	echo "d.add('$parent/$dir','$parent','$dir','javascript:loadfiles(\'".base64_encode(PATH."$parent/$dir")."\');', 'View files ".PATH."$parent/$dir"."', '','img/folder.gif','img/folderopen.gif');\n";
           	read_dir(PATH."/$parent/$dir","$parent/$dir");
		} else {
			echo "d.add('$parent/$dir','$parent','$dir','javascript:loadfiles(\'".base64_encode(PATH."/$dir")."\');', 'View files ".PATH."/$dir"."', '','img/folder.gif','img/folderopen.gif');\n";
			read_dir(PATH."/$dir","$parent/$dir");
		}
       }
   }
   if ($path) closedir($path);
   return $output;
}

/*FUNCTIONS END*/


define("APPVERSION","1.0");
define("FILENAMEMAXLENGTH","18");
define("CREDIT","By Alex, Marian and Sean.");

?>
